package com.cop4331.social;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * @author Matt Szoke
 * @LastUpdated: 3/5/2012 by Matt Szoke
 * 
 * Logs in via PHP script to mySQL server
 * 
 */
public class LoginActivity extends Activity {

	// Login Variables
	EditText user;
	EditText pw;
	TextView err;
	Button login;
	Button register;
	Button fp;
	JSONObject CurrentUser;
	Button help_login;
	Button help_password;

	// -----------------------------------------------
	// ONCREATE
	// -----------------------------------------------
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		UserFunctions uf = new UserFunctions();

		// Edit Text Fields
		user = (EditText) findViewById(R.id.et_un);
		pw = (EditText) findViewById(R.id.et_pw);

		// Error Display to User
		err = (TextView) findViewById(R.id.tv_error);

		// Buttons
		login = (Button) findViewById(R.id.btn_login);
		register = (Button) findViewById(R.id.register);
		fp = (Button) findViewById(R.id.b_fp);

		// Help (Dialog) Buttons
		help_login = (Button) findViewById(R.id.help_login);
		help_password = (Button) findViewById(R.id.help_password);

		// Gives functionality to help_login button
		help_login.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Dialog dialog = new Dialog(LoginActivity.this);

				dialog.setContentView(R.layout.help_dialog);
				dialog.setTitle("Login Help: ");
				dialog.setCancelable(true);

				// Create and show a dialog to help the user with this specific
				// field
				TextView text = (TextView) dialog.findViewById(R.id.text);
				text.setText("Please enter your username into this field.\n\nIf you do not have a username, please hit the register button below.");
				text.setGravity(Gravity.CENTER);
				dialog.setCanceledOnTouchOutside(true);
				dialog.show();

			}
		});

		// Gives functionality to the help_password button
		help_password.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Dialog dialog = new Dialog(LoginActivity.this);

				dialog.setContentView(R.layout.help_dialog);
				dialog.setTitle("Login Help: ");
				dialog.setCancelable(true);

				// Create and show a dialog to help the user with this specific
				// field
				TextView text = (TextView) dialog.findViewById(R.id.text);
				text.setText("Please enter your password into this field.\n\nIf you do not remember your password, please hit forgot password to recover.");
				text.setGravity(Gravity.CENTER);
				dialog.setCanceledOnTouchOutside(true);
				dialog.show();

			}
		});

		// Gives functionality to Register button
		register.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				// Goes to Register activity
				Intent reg = new Intent(getApplicationContext(),
						RegisterActivity.class);
				startActivity(reg);
				finish();

			}
		});

		// Gives functionality to the ForgotPassword button
		fp.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				// Goes to ForgotPassword Activity
				Intent fpa = new Intent(getApplicationContext(),
						ForgotpassActivity.class);
				startActivity(fpa);
				finish();

			}
		});

		// Give functionality to Login Button
		login.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				// Retreive information from User input fields
				String un = user.getText().toString();
				String password = pw.getText().toString();

				UserFunctions userFunction = new UserFunctions();
				JSONObject json = userFunction.loginUser(un, password);

				// Check for Login Response
				try {
					if (json.getString("success") != null) {

						err.setText("");
						String res = json.getString("success");

						if (Integer.parseInt(res) == 1) {

							// User successfully logged in
							// Create SQLite Database for App Session
							DatabaseHandler db = new DatabaseHandler(
									getApplicationContext());
							db.resetTables();
							JSONObject json_user = json.getJSONObject("user");

							// Clear all previous data in database
							userFunction.logoutUser(getApplicationContext());

							Intent dashboard = new Intent(
									getApplicationContext(),
									DashBoardActivity.class);

							// Add logged in user
							db.addUser(json_user.getString("name"));

							// Close all views before launching DashBoard
							dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							startActivity(dashboard);

							// Close Login Screen
							finish();
						} else {
							// Error in login
							err.setText(json.getString("error_msg"));
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});
	}
}
